package com.gp.controller;

import com.google.common.base.Strings;
import com.gp.entity.Admin;
import com.gp.service.AdminService;
import com.gp.utils.MD5Utils;
import com.gp.utils.MapControl;
import com.gp.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/regist")
    public String v_regist(){
        return "register";
    }
    @PostMapping("/regist")
    @ResponseBody
    public Map<String, Object> regist(@RequestBody Admin admin){
        admin.setPassword(MD5Utils.getMD5(admin.getPassword()));
        int result = adminService.create(admin);
        if(result<=0){
            //失败的情况下
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/accountCheck")
    @ResponseBody
    public Map<String, Object> accCheck(@RequestParam("account") String account){
        //System.out.println("====="+account);
        Admin admin = new Admin();
        admin.setAccount(account);
        List<Admin> l = adminService.accountCheck(admin);
        if(l.size()>0){
            return MapControl.getInstance().error().getMap();
        }else {
            return MapControl.getInstance().success().getMap();
        }
    }

    @GetMapping("/login")
    public String v_login(){
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String account = map.get("account")+"";
        String password = map.get("password")+"";
        if(Strings.isNullOrEmpty(account) || Strings.isNullOrEmpty(password)){
            return MapControl.getInstance().error("用户名或密码不能为空").getMap();
        }
        Admin admin = adminService.login(account, MD5Utils.getMD5(password));
        if(admin != null){
            SessionUtils.setAdmin(request,admin);
            return MapControl.getInstance().success().getMap();
        }else{
            return MapControl.getInstance().error("用户名或密码错误").getMap();
        }
    }

    @GetMapping("/pwd")
    public String pwd(){
        return "pwd";
    }
    @PostMapping("/pwd")
    @ResponseBody
    public Map<String,Object> pwd(Integer id,String type,String sourcePwd,String newPwd){
        Admin user = adminService.detail(id);
        if(user.getPassword().equals(MD5Utils.getMD5(sourcePwd))){
            Admin entity = new Admin();
            entity.setId(id);
            entity.setPassword(MD5Utils.getMD5(newPwd));
            int update = adminService.update(entity);
            if(update>0){
                return MapControl.getInstance().success().getMap();
            }else{
                return MapControl.getInstance().error().getMap();
            }
        }else{
            return MapControl.getInstance().error("原密码错误").getMap();
        }
    }

    @GetMapping("/out")
    public void out(HttpServletRequest request){
        SessionUtils.closeSession(request);
        //System.out.println("退出了");
    }
}
